package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.heatedloan.baseservice.dal.mapper.PictureConfigMapper;
import com.nuanshui.heatedloan.baseservice.facade.AdvertisementService;
import com.nuanshui.heatedloan.baseservice.facade.PictureConfigService;
import com.nuanshui.heatedloan.baseservice.model.SystemPicture;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.constant.SystemPictureType;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.pageable.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(ServiceConstant.PICTURECONFIGSERVICE)
@Transactional(readOnly=true,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
public class PictureConfigServiceImpl extends CommonServiceImpl implements PictureConfigService {

	
	public static final String ScreenSize_IPhone6P = "ios_5.5";
	public static final String ScreenSize_IPhone6 = "ios_4.7";
	public static final String ScreenSize_IPhone5 = "ios_4.0";
	public static final String ScreenSize_IPhone4 = "ios_3.5";
	
	
	public static final String LaunchImgFormat_IPhone6P = "?imageMogr2/crop/!1242x1856a0a0";
	public static final String LaunchImgFormat_IPhone6 = "?imageMogr2/crop/!750x1156a1242a0";
	public static final String LaunchImgFormat_IPhone5 = "?imageMogr2/crop/!640x953a1992a0";
	public static final String LaunchImgFormat_IPhone4 = "?imageMogr2/crop/!640x785a2632a0";

	//显示安卓手机1080*1920
	public static final String LaunchImgFormat_Android_1920 = "?imageMogr2/crop/!1080x1652a3272a0";
	//显示默认手机
	public static final String LaunchImgFormat_Android_1080 = "?imageMogr2/crop/!1080x1604a4352a0";
	//显示安卓手机720-1089
	public static final String LaunchImgFormat_Android_720 = "?imageMogr2/crop/!720x1089a5432a0";

    @Autowired
    private PictureConfigMapper pictureConfigMapper;
    
    @Autowired
	private ResourceFacade resourceFacade;

	@Override
	public SystemPicture findFirstLaunchPicture(String screenSize) {
		SystemPicture item=null;
		List<SystemPicture> launchPics = findLaunchPicture(screenSize);
		if (launchPics.size() > 0) {
			item=launchPics.get(0);
		}
		return item;
	}

	@Override
	public List<SystemPicture> findLaunchPicture(String screenSize) {
		List<SystemPicture> launchPics = findSystemPicture(SystemPictureType.LAUNCH_IMAGE);
		for (SystemPicture item :launchPics) {
			fetchLaunchImgUrl(screenSize,item);
		}
		return launchPics;
	}
	
	public List<SystemPicture> findSystemPicture(String type) {
		return pictureConfigMapper.findSystemPicture(type);
	}

	private void fetchLaunchImgUrl(String screenSize,SystemPicture item)
	{
		String key = item.getAvatar();
		String strImg = resourceFacade.resolveUrl(key, false);
		//判断兼容性,3.0版本以下就要用转储图片
		String strLaunchImgUrl = selectLaunchImgUrl(screenSize,strImg);
		item.setLaunchImageUrl(strLaunchImgUrl);
	}
	
	private String selectLaunchImgUrl(String screenSize,String img) {
		String strSize = null;
		strSize = validateScreenSize(screenSize);
		StringBuilder sb = new StringBuilder(img);
		String scrSize = strSize.trim();
		int size = 0;

		if (scrSize.startsWith("ios")) {

		} else {
			//转换成int类型
			size = Integer.parseInt(scrSize);
		}
		String strImgFormat=LaunchImgFormat_Android_720;
		if (scrSize.equals(ScreenSize_IPhone6P)) {
			strImgFormat=LaunchImgFormat_IPhone6P;
		} else if (scrSize.equals(ScreenSize_IPhone6)) {
			strImgFormat=LaunchImgFormat_IPhone6;
		} else if (scrSize.equals(ScreenSize_IPhone5)) {
			strImgFormat=LaunchImgFormat_IPhone5;
		} else if (scrSize.equals(ScreenSize_IPhone4)) {
			strImgFormat=LaunchImgFormat_IPhone4;
		} else if (!scrSize.startsWith("ios") && size >= 1920) {
			strImgFormat=LaunchImgFormat_Android_1920;
		} else if (!scrSize.startsWith("ios") && size > 1089 && size < 1920) {
			strImgFormat=LaunchImgFormat_Android_1080;
		}

		sb.append(strImgFormat);
		return sb.toString();
	}

	@Override
	public String validateScreenSize(String screenSize) {
    	String strSize = null;
		if (screenSize.endsWith("x")) {
			strSize = "0"; //赋予一个默认值
		} else {
			if (screenSize.contains("x") || screenSize.startsWith("x")) {
				String[] sSize = screenSize.split("x");
				if (sSize != null && sSize.length > 0) {
					strSize = sSize[1]; //根据屏幕高度计算
				} else {
					strSize = "0"; //赋予一个默认值
				}
			} else {
				strSize = screenSize;
			}
		}
		return strSize;
	}

	@Override
	public Pager<SystemPicture> listLoadPicture(Pager<SystemPicture> pager) {
		pager.setData(pictureConfigMapper.listLoadPicture(pager));
		return pager;
	}

	@Override
	public Integer getLoadPictureCount() {
		return pictureConfigMapper.getLoadPictureCount();
	}

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void save(SystemPicture picture) {

		pictureConfigMapper.save(picture);
	}

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void updateSystemKey(SystemPicture picture) {
		pictureConfigMapper.updateSystemKey(picture);
	}

	@Override
	@Transactional(readOnly=false,value=TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void delete(String id) {
		pictureConfigMapper.delete(id);
	}


//	Advertisement convert(SystemPicture picture)
//	{
//		Advertisement item=new Advertisement();
//		item.setBizType(SystemAdvertisementBizType.ADVERTISEMENT_LAUNCHIMAGE);
//
//	}
}
